package org.winexpress

import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONObject
import org.codehaus.groovy.grails.web.json.JSONArray

class WineVarietalController {
    // shared methods
    def sendResponse(statusCode) {
        if (statusCode == 404) {
            JSONObject o = new JSONObject()
            o.put(statusCode, true)
            render o as JSON
        }
        
        response.status = statusCode
        render response
        
        // 200-OK (e.g. GET success)
        // 201-Created (e.g. POST success)
        // 403-Forbidden (e.g. LOGIN failure)
        // 400-Bad Request (e.g. PUT,POST,DELETE failure)
        // 404-Not Found (e.g. GET failure)
        // 500-Internal Server Error (other...)
    }
    
    

    // REST operations
    // GET
    def show = {
//        println 'WineVarietalController >> Show >> START'
        
        WineVarietalManagement wvman = new WineVarietalManagement()
        
        if (params.id && WineVarietal.exists(params.id)) {    
            def wv = wvman.getWineVarietalByID(params.id)
            render wv as JSON
            sendResponse(200)
            
//            println 'Show wine varietal by ID >> ' + wv.id + " " + wv
        }
        else {
            def allWV = wvman.listWineVarietals()
            if (allWV.length() > 0) {
                render allWV as JSON
                sendResponse(200)
            }
            else {
                sendResponse(404)
            } 
            
//            println 'Listing all wine cats >> ' + allWV
        }
        
//        println 'WineVarietalController >> Show >> END'
    }
    
    // PUT
    def update = {
        println 'WineVarietalController >> Update >> START'
        
        JSONObject o = request.JSON
        
        WineVarietalManagement wvman = new WineVarietalManagement()
        
        def wv = wvman.getWineVarietalByID(o.get("id"))
        if (o.has("wineVarietalName")) { wv.wineVarietalName = o.get("wineVarietalName"); println wv.wineVarietalName }
        if (o.has("description")) { wv.description = o.get("description"); println wv.description }
        if (o.has("wineType")) { wv.wineType = o.get("wineType") }
//        println "CONTROLLER: " + wv.wineType
        
        def result = wvman.updateWineVarietal(wv)
        if (result == true) {
            println 'WineVarietalController >> Update >> ' + result
            sendResponse(200)
        }
        else {
            println 'WineVarietalController >> Update >> ' + result
            sendResponse(400)
        }
        

        println 'WineVarietalController >> Update >> END'

    }
    
    // DELETE
    def delete = {
        println 'WineVarietalController >> delete >> START'
        
        WineVarietalManagement wvman = new WineVarietalManagement()
        
        if (params.id && WineVarietal.exists(params.id)) {    
            def result = wvman.deleteWineVarietal(id)
            
            if (result == true) {
                println 'WineVarietalController >> delete >> ' + result
                sendResponse(200)
            }
            else {
                println 'WineVarietalController >> delete >> ' + result
                sendResponse(404)
            }
        }
        
        println 'WineVarietalController >> delete >> END'
    }
    
    // POST
    def save = {
        println 'WineVarietalController >> Save >> START'
        
        JSONObject o = request.JSON
        println o
        
        WineVarietalManagement wvman = new WineVarietalManagement()
        
        if (isUniqueWineVarietalName(o.get("wineVarietalName")) == true) {
            def result = wvman.addWineVarietal(o)
            if (result == true) {
                println 'WineVarietalController >> Save >> ' + result
                sendResponse(201)
            }
            else {
                println 'WineVarietalController >> Save >> ' + result
                sendResponse(400)
            }
        }
        else {
            sendResponse(406)
        }
        
        println 'WineVarietalController >> Save >> END'
    }
    
    
    // supporting operations
    def isUniqueWineVarietalName(wineVarietalName) {
        
        def status = false
        
        WineVarietalManagement wvman = new WineVarietalManagement()
        WineVarietal wv = null
        
        wv = wvman.getWineVarietalForDbByWineVarietalName(wineVarietalName)
        
        if (wv == null) {
            status = true 
        }
        
        return status
    }
    
}
